package com.workstudy.web.controller.workstudy;

import java.util.List;
import java.util.stream.Collectors;

import com.workstudy.common.core.domain.entity.SysRole;
import com.workstudy.common.utils.ShiroUtils;
import com.workstudy.workstudy.domain.WorkstudyPost;
import com.workstudy.workstudy.service.IWorkstudyPostService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.workstudy.common.annotation.Log;
import com.workstudy.common.enums.BusinessType;
import com.workstudy.workstudy.domain.WorkstudyPostUser;
import com.workstudy.workstudy.service.IWorkstudyPostUserService;
import com.workstudy.common.core.controller.BaseController;
import com.workstudy.common.core.domain.AjaxResult;
import com.workstudy.common.utils.poi.ExcelUtil;
import com.workstudy.common.core.page.TableDataInfo;

/**
 * 学生申请岗位Controller
 * 
 * @author ws
 * @date 2021-03-28
 */
@Controller
@RequestMapping("/workstudy/post_user")
public class WorkstudyPostUserController extends BaseController
{
    private String prefix = "workstudy/post_user";

    @Autowired
    private IWorkstudyPostUserService workstudyPostUserService;
    @Autowired
    private IWorkstudyPostService workstudyPostService;

    @RequiresPermissions("workstudy:post_user:view")
    @GetMapping()
    public String post_user()
    {
        return prefix + "/post_user";
    }

    /**
     * 查询学生申请岗位列表
     */
    @RequiresPermissions("workstudy:post_user:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WorkstudyPostUser workstudyPostUser)
    {
        //获取登录的用户role
        List<SysRole> roleList = ShiroUtils.getSysUser().getRoles();
        for(SysRole role:roleList){
            //如果是学生就查出他自己的岗位
            if("student".equals(role.getRoleKey())){
                workstudyPostUser.setUserId(ShiroUtils.getUserId());
            }
            //如果是用人单位就查出他指导的岗位
            else if("employer".equals(role.getRoleKey())){
                workstudyPostUser.setApproveEndBy(ShiroUtils.getLoginName());
            }
        }
        startPage();
        List<WorkstudyPostUser> list = workstudyPostUserService.selectWorkstudyPostUserList(workstudyPostUser);
        return getDataTable(list);
    }

    /**
     * 导出学生申请岗位列表
     */
    @RequiresPermissions("workstudy:post_user:export")
    @Log(title = "学生申请岗位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WorkstudyPostUser workstudyPostUser)
    {
        List<WorkstudyPostUser> list = workstudyPostUserService.selectWorkstudyPostUserList(workstudyPostUser);
        ExcelUtil<WorkstudyPostUser> util = new ExcelUtil<WorkstudyPostUser>(WorkstudyPostUser.class);
        return util.exportExcel(list, "post_user");
    }

    /**
     * 新增学生申请岗位
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        WorkstudyPost workstudyPost=new WorkstudyPost();
        workstudyPost.setStatus("1");
        List<WorkstudyPost> postList = workstudyPostService.selectWorkstudyPostList(workstudyPost);
        mmap.put("postList", postList);
        return prefix + "/add";
    }

    /**
     * 新增保存学生申请岗位
     */
    @RequiresPermissions("workstudy:post_user:add")
    @Log(title = "学生申请岗位", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WorkstudyPostUser workstudyPostUser)
    {

        int bycount = workstudyPostUserService.selectWorkstudyPostUserBycount(workstudyPostUser);
        if(bycount>0){
            return error("已提交过此岗位的申请！");
        }
        workstudyPostUser.setUserId(ShiroUtils.getUserId());
        return toAjax(workstudyPostUserService.insertWorkstudyPostUser(workstudyPostUser));
    }

    /**
     * 修改学生申请岗位
     */
    @GetMapping("/edit/{postUserId}")
    public String edit(@PathVariable("postUserId") Long postUserId, ModelMap mmap)
    {
        WorkstudyPostUser workstudyPostUser = workstudyPostUserService.selectWorkstudyPostUserById(postUserId);
        mmap.put("workstudyPostUser", workstudyPostUser);
        WorkstudyPost workstudyPost=new WorkstudyPost();
        workstudyPost.setStatus("1");
        List<WorkstudyPost> postList = workstudyPostService.selectWorkstudyPostList(workstudyPost);
        mmap.put("postList", postList);
        return prefix + "/edit";
    }

    /**
     * 修改保存学生申请岗位
     */
    @RequiresPermissions("workstudy:post_user:edit")
    @Log(title = "学生申请岗位", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WorkstudyPostUser workstudyPostUser)
    {
        workstudyPostUser.setUserId(ShiroUtils.getUserId());
        return toAjax(workstudyPostUserService.updateWorkstudyPostUser(workstudyPostUser));
    }

    /**
     * 删除学生申请岗位
     */
    @RequiresPermissions("workstudy:post_user:remove")
    @Log(title = "学生申请岗位", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(workstudyPostUserService.deleteWorkstudyPostUserByIds(ids));
    }

    /**
     * 修改审核
     */
    @GetMapping("/appr/{id}")
    public String appr(@PathVariable("id") Long id, ModelMap mmap)
    {
        WorkstudyPostUser workstudyPostUser = workstudyPostUserService.selectWorkstudyPostUserById(id);
        mmap.put("workstudyPostUser", workstudyPostUser);
        return prefix + "/appr";
    }

    /**
     * 审核
     */
    @RequiresPermissions("workstudy:post_user:appr")
    @Log(title = "岗位", businessType = BusinessType.UPDATE)
    @PostMapping("/appr")
    @ResponseBody
    public AjaxResult appr(WorkstudyPostUser workstudyPostUser)
    {
        workstudyPostUser.setApproveBy(ShiroUtils.getLoginName());
        return toAjax(workstudyPostUserService.updateWorkstudyPostUserAppr(workstudyPostUser));
    }

    /**
     * 修改审核
     */
    @GetMapping("/appr2/{id}")
    public String appr2(@PathVariable("id") Long id, ModelMap mmap)
    {
        WorkstudyPostUser workstudyPostUser = workstudyPostUserService.selectWorkstudyPostUserById(id);
        mmap.put("workstudyPostUser", workstudyPostUser);
        return prefix + "/appr2";
    }

    /**
     * 审核
     */
    @RequiresPermissions("workstudy:post_user:appr2")
    @Log(title = "岗位", businessType = BusinessType.UPDATE)
    @PostMapping("/appr2")
    @ResponseBody
    public AjaxResult appr2(WorkstudyPostUser workstudyPostUser)
    {
        workstudyPostUser.setApproveEndBy(ShiroUtils.getLoginName());
        return toAjax(workstudyPostUserService.updateWorkstudyPostUserAppr2(workstudyPostUser));
    }
}
